﻿using System;
using System.Xml.Serialization;

namespace eProject.Model
{
    [XmlRoot("ScheduleCommonSettings")]
    public class ScheduleSettings
    {

        public ScheduleSettings()
        {
            Monday = 8;
            Tuesday = 8;
            Wednesday = 8;
            Thursday = 8;
            Friday = 8;
            Satureday = 0;
            Sunday = 0;
        }

        [XmlAttribute("Mo")]
        public double Monday
        {
            get { return _monday; }
            set
            {
                if (value == _monday) return;
                _monday = value;
                InvokeSettingsChanged(this);
            }
        }

        [XmlAttribute("Tu")]
        public double Tuesday
        {
            get { return _tuesday; }
            set
            {
                if (value == _tuesday) return;
                _tuesday = value;
                InvokeSettingsChanged(this);
            }
        }

        [XmlAttribute("We")]
        public double Wednesday
        {
            get { return _wednesday; }
            set
            {
                if (value == _wednesday) return;
                _wednesday = value;
                InvokeSettingsChanged(this);
            }
        }

        [XmlAttribute("Th")]
        public double Thursday
        {
            get { return _thursday; }
            set
            {
                if (value == _thursday) return;
                _thursday = value;
                InvokeSettingsChanged(this);
            }
        }

        [XmlAttribute("Fr")]
        public double Friday
        {
            get { return _friday; }
            set
            {
                if (value == _friday) return;
                _friday = value;
                InvokeSettingsChanged(this);
            }
        }

        [XmlAttribute("Sa")]
        public double Satureday
        {
            get { return _satureday; }
            set
            {
                if (value == _satureday) return;
                _satureday = value;
                InvokeSettingsChanged(this);
            }
        }

        [XmlAttribute("Su")]
        public double Sunday
        {
            get { return _sunday; }
            set
            {
                if (value == _sunday) return;
                _sunday = value;
                InvokeSettingsChanged(this);
            }
        }

        public double GetManHours(DateTime date)
        {
            return GetManHours(date.DayOfWeek);
        }

        public double GetManHours(DayOfWeek dayOfWeek)
        {
            switch (dayOfWeek)
            {
                case DayOfWeek.Monday:
                    return Monday;

                case DayOfWeek.Tuesday:
                    return Tuesday;

                case DayOfWeek.Wednesday:
                    return Wednesday;

                case DayOfWeek.Thursday:
                    return Thursday;

                case DayOfWeek.Friday:
                    return Friday;

                case DayOfWeek.Saturday:
                    return Satureday;

                case DayOfWeek.Sunday:
                    return Sunday;

            }
            throw new ArgumentOutOfRangeException();
        }

        public void SetManHours(DayOfWeek dayOfWeek, double hours)
        {
            switch (dayOfWeek)
            {
                case DayOfWeek.Monday:
                    Monday = hours;
                    return;

                case DayOfWeek.Tuesday:
                    Tuesday = hours;
                    return;

                case DayOfWeek.Wednesday:
                    Wednesday = hours;
                    return;

                case DayOfWeek.Thursday:
                    Thursday = hours;
                    return;

                case DayOfWeek.Friday:
                    Friday = hours;
                    return;

                case DayOfWeek.Saturday:
                    Satureday = hours;
                    return;

                case DayOfWeek.Sunday:
                    Sunday = hours;
                    return;

            }
            throw new ArgumentOutOfRangeException();
        }

        public void Load(ScheduleSettings settings)
        {
            Monday = settings.Monday;
            Tuesday = settings.Tuesday;
            Wednesday = settings.Wednesday;
            Thursday = settings.Thursday;
            Friday = settings.Friday;
            Satureday = settings.Satureday;
            Sunday = settings.Sunday;
        }

        public event EventHandler<ScheduleSettings> SettingsChanged;

        private void InvokeSettingsChanged(ScheduleSettings item)
        {
            var handler = SettingsChanged;
            if (handler != null) handler(item);
        }


        private double _monday;
        private double _tuesday;
        private double _wednesday;
        private double _thursday;
        private double _friday;
        private double _satureday;
        private double _sunday;
    }
}
